/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBiseccion.java
 *
 * Created on 11-oct-2010, 14:12:47
 */
package interfaz.FuncionPanels;

//import java.awt.FlowLayout;
//import javax.print.DocFlavor.STRING;
//import javax.management.monitor.GaugeMonitor;
import interfaz.NuMethodApp;
import java.awt.Color;
import javax.swing.JFrame;
//import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author The NuMethod Team
 */
public class PanelInterpolacion extends javax.swing.JPanel {

    private static DefaultTableModel ma;

    private static Double value = 0d;
    public PanelInterpolacion() {
        ma = new DefaultTableModel();
        initComponents();
        initComponents2();
    }

    // <editor-fold defaultstate="collapsed" desc="Helpful getters">
    public static DefaultTableModel getMa() {
        return ma;
    }

    // </editor-fold>
    private void initComponents2() {
        //this.jTable1.setVisible(false);
        TablePuntos.setShowGrid(true);
        TablePuntos.setOpaque(true);
        TablePuntos.setAutoStartEditOnKeyStroke(true);
        TablePuntos.setRolloverEnabled(true);
        TablePuntos.setSurrendersFocusOnKeystroke(true);
        TablePuntos.setTerminateEditOnFocusLost(true);
        TablePuntos.setFillsViewportHeight(true);


        TablePuntos.setVisibleColumnCount(0);

        rebuildTableModels(3);


    }

    /**
     * Metodo que reconstruye de forma dinamica los modelos de tabla
     * luego de cada entrada de datos
     * @param a el numero de ecuaciones del sistema
     */
    private void rebuildTableModels(Integer a) {
        //Constuir el modelo para la tabla A
        Double[][] test = new Double[2][a];
        String[] names = new String[a];

        for (int i = 0; i < a; i++) {
            String aTest = "Punto (x" + (i) + "," + "y" + (i) + ")";
            names[i] = aTest;
        }

        ma = new DefaultTableModel(test, names);
        TablePuntos.setModel(ma);

        ma.fireTableDataChanged();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        TablePuntos = new org.jdesktop.swingx.JXTable();
        jLabel4 = new javax.swing.JLabel();
        FieldMatriz2 = new javax.swing.JTextField();
        LabelStatus = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        FieldValue = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setToolTipText("Interpolacion");
        setPreferredSize(new java.awt.Dimension(640, 580));

        TablePuntos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TablePuntos);

        jLabel4.setText("Matriz A");

        FieldMatriz2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                FieldMatriz2MouseClicked(evt);
            }
        });
        FieldMatriz2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FieldMatriz2ActionPerformed(evt);
            }
        });

        LabelStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/Icons16/idea.png"))); // NOI18N
        LabelStatus.setText("Inserta el # Puntos del Sistema, y presiona Enter");
        LabelStatus.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        jLabel1.setText("Interpolacion");

        FieldValue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                FieldValueMouseExited(evt);
            }
        });
        FieldValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FieldValueActionPerformed(evt);
            }
        });
        FieldValue.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                FieldValueFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                FieldValueFocusLost(evt);
            }
        });

        jLabel2.setText("Coloca un punto a evaluar y presiona enter");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(FieldMatriz2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(LabelStatus))
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(FieldValue, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(194, 194, 194)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FieldMatriz2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LabelStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addGap(2, 2, 2)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FieldValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(208, 208, 208))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Metodo que extrae de los textfield`s los valores de los parametros
     * para los metodos iterativos.
     */
    private void FieldMatriz2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FieldMatriz2ActionPerformed
        try {
            Integer a = Integer.parseInt(this.FieldMatriz2.getText());
            if (a > 0) {
                rebuildTableModels(a);

            } else {
                LabelStatus.setText("El numero de puntos debe ser mayor a 0");
            }

        } catch (NumberFormatException numberFormatException) {
            LabelStatus.setText("Imposible parsear numero, intenta otra vez");
        }
    }//GEN-LAST:event_FieldMatriz2ActionPerformed

    private void FieldMatriz2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FieldMatriz2MouseClicked
        LabelStatus.setText("Inserta el # de puntos del sistema");
    }//GEN-LAST:event_FieldMatriz2MouseClicked

    private void FieldValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FieldValueActionPerformed
        try {
            value = Double.parseDouble(FieldValue.getText());
            jLabel2.setText("Hecho");
            jLabel2.setForeground(Color.green);
        } catch (NumberFormatException numberFormatException) {
            LabelStatus.setText("Error en el numero");
            jLabel2.setText("Error");
            jLabel2.setForeground(Color.red);
        }
    }//GEN-LAST:event_FieldValueActionPerformed

    private void FieldValueFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_FieldValueFocusGained
        jLabel2.setText("Coloca un punto a evaluar y presiona enter");
        jLabel2.setForeground(Color.black);
    }//GEN-LAST:event_FieldValueFocusGained

    private void FieldValueFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_FieldValueFocusLost
       jLabel2.setText("Coloca un punto a evaluar y presiona enter");
        jLabel2.setForeground(Color.black);
    }//GEN-LAST:event_FieldValueFocusLost

    private void FieldValueMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FieldValueMouseExited
        jLabel2.setText("Coloca un punto a evaluar y presiona enter");
        jLabel2.setForeground(Color.black);
    }//GEN-LAST:event_FieldValueMouseExited

    public static Double getValue (){
        return value;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField FieldMatriz2;
    private javax.swing.JTextField FieldValue;
    private javax.swing.JLabel LabelStatus;
    private org.jdesktop.swingx.JXTable TablePuntos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {
        JFrame myFrame = new JFrame("Test");
        PanelInterpolacion m = new PanelInterpolacion();
        //myFrame.setLayout(new FlowLayout());
        myFrame.add(m);

        myFrame.pack();
        myFrame.setVisible(true);


    }
}
